package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.generator.IFill;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.Category;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 分类品牌 服务实现类
 * </p>
 *
 * @author lee
 * @since 2024-07-13
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {


    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {
        return baseMapper.selectCategoryBrandList(categoryBrand);
    }

    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {

        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId());
        Long count = baseMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new ServiceException("该分类品牌已存在");
        }
        categoryBrand.setCreateBy(SecurityUtils.getUsername());
        return baseMapper.insert(categoryBrand);
    }

    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        CategoryBrand categoryBrandDB = baseMapper.selectById(categoryBrand.getId());
        if (categoryBrandDB != null) {
            if (categoryBrandDB.getBrandId().equals(categoryBrand.getBrandId()) &&
                    categoryBrandDB.getCategoryId().equals(categoryBrand.getCategoryId())) {
                return 1;
            }
        }
        LambdaQueryWrapper<CategoryBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId());
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new ServiceException("该分类品牌已存在");
        }
        categoryBrand.setUpdateBy(SecurityUtils.getUsername());
        return baseMapper.updateById(categoryBrand);
    }

    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = baseMapper.selectById(id);
        if (categoryBrand!=null) {
            List ids = orderCategoryIdList(new ArrayList(), categoryBrand.getCategoryId());
//            List<Long> categoryIdList = getCategoryIdList(new ArrayList(), categoryBrand.getCategoryId());
//            List<Long> ids = orderCategoryIdList(categoryIdList);
            categoryBrand.setCategoryIdList(ids);
        }
        return categoryBrand;
    }

    public List<Long> getCategoryIdList(List list, Long categoryId) {
        if (categoryId > 0) {
            LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .select(Category::getId, Category::getParentId)
                    .eq(Category::getId, categoryId);
            Category category = categoryMapper.selectOne(queryWrapper);
            list.add(category.getId());

            getCategoryIdList(list, category.getParentId());
        }

        return list;
    }

    @Override
    public List<Long> orderCategoryIdList(List<Long> list,Long id) {
        List<Long> categoryIdList = getCategoryIdList(list, id);
        ArrayList<Long> ids = new ArrayList<>();
        if (categoryIdList != null) {
            for (int i = categoryIdList.size()-1; i >= 0; i--) {
                ids.add(categoryIdList.get(i));
            }
        }
        return ids;
    }

    @Override
    public List<Brand> getBrandList(Long id) {
        return baseMapper.getBrandList(id);
    }
}
